﻿using LogTestApp.Commands;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;


{
    var services = new ServiceCollection();
    var configuration = new ConfigurationManager();
    configuration.SetBasePath(Directory.GetCurrentDirectory());
    configuration.AddJsonFile("appsettings.json");

    services.AddLogging(configure =>
    {
        configure.AddConsole();
        configure.AddDebug();

        //configure.AddFilter((provider, categorName, level) => {
        //    return categorName.StartsWith("A.B") && level >= LogLevel.Information;
        //});

        configure.AddConfiguration(configuration.GetSection("Logging"));
    });

    services.AddMediatR(opt =>
    {
        opt.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ExceptionPipelineBehavior<,>), ServiceLifetime.Scoped);
        opt.AddBehavior(typeof(IPipelineBehavior<,>), typeof(TrsanctionPipelineBehavior<,>), ServiceLifetime.Scoped);
        opt.RegisterServicesFromAssemblyContaining<Program>();
        opt.Lifetime = ServiceLifetime.Scoped;
    });

    var sp = services.BuildServiceProvider();

    var mediator = sp.GetRequiredService<IMediator>();

    await mediator.Send(new CreateStudentCommand() {Name = "张三"});
}
